﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

class MainMenuScreen : MenuScreen
{
    SpriteFont font;

    private Texture2D backgroundTexture;
    private Vector2 backgroundPosition;

    private Texture2D imageTexture;
    private Vector2 imagePosition;
    private Vector2 descriptionAreaTextPosition;

    private Texture2D gameLogoTexture;
    private Vector2 gameLogoPosition;

    private Texture2D bButtonTexture;
    private Vector2 bButtonPosition;

    private Texture2D aButtonTexture;
    private Vector2 aButtonPosition;

    private Texture2D buttonStartTexture, buttonOptionsTexture, buttonCreditsTexture;
    
    MenuEntry gameMenuEntry, exitMenuEntry, optionsMenuEntry, creditsMenuEntry;

    /// <summary>
    /// Constructor fills in the menu contents.
    /// </summary>
    public MainMenuScreen()
        : base()
    {
        Direction = MenuEntryDirection.HORIZONTAL;

        selectedEntry = 1;

        optionsMenuEntry = new MenuEntry("");
        optionsMenuEntry.Description = "View Game Options";
        optionsMenuEntry.Position = new Vector2(0, 0f);
        optionsMenuEntry.Selected += OptionsMenuEntrySelected;
        MenuEntries.Add(optionsMenuEntry);

        gameMenuEntry = new MenuEntry("");
        gameMenuEntry.Description = "Start a New Game";
        gameMenuEntry.Position = new Vector2(251, 0f);
        gameMenuEntry.Selected += NewGameMenuEntrySelected;
        MenuEntries.Add(gameMenuEntry);

        creditsMenuEntry = new MenuEntry("");
        creditsMenuEntry.Description = "View Game Credits";
        creditsMenuEntry.Position = new Vector2(872, 0f);
        creditsMenuEntry.Selected += CreditsMenuEntrySelected;
        MenuEntries.Add(creditsMenuEntry);

        exitMenuEntry = new MenuEntry("Exit");
        exitMenuEntry.Description = "Quit the Game";
        exitMenuEntry.Position = new Vector2(950, 600f);
        exitMenuEntry.Selected += OnCancel;
        //MenuEntries.Add(exitMenuEntry);
    }


    /// <summary>
    /// Load the graphics content for this screen.
    /// </summary>
    public override void LoadContent()
    {
        base.LoadContent();

        // load the textures
        ContentManager content = ScreenManager.Game.Content;

        font = content.Load<SpriteFont>("SpriteFont1");

        //set font on each entry menu
        gameMenuEntry.Font = font;
        optionsMenuEntry.Font = font;
        creditsMenuEntry.Font = font;
        exitMenuEntry.Font = font;

        backgroundTexture = content.Load<Texture2D>(@"Textures\MenuMock\menu_inicial_2Cinza");
        imageTexture =
            content.Load<Texture2D>(@"Textures\MenuMock\imagemdojogo");

        buttonCreditsTexture = content.Load<Texture2D>(@"Textures\MenuMock\menu_credits");

        buttonOptionsTexture = content.Load<Texture2D>(@"Textures\MenuMock\menu_option");

        buttonStartTexture = content.Load<Texture2D>(@"Textures\MenuMock\Menu_start");

        //gameLogoTexture = content.Load<Texture2D>(@"Textures\MainMenu\GameLogo");
        /*button1Texture =
            content.Load<Texture2D>(@"Textures\MenuMock\botao");
        button1SelectedTexture =
            content.Load<Texture2D>(@"Textures\MenuMock\botaoselecionado");*/
       
        //bButtonTexture = content.Load<Texture2D>(@"Textures\Buttons\BButton");
        //aButtonTexture = content.Load<Texture2D>(@"Textures\Buttons\AButton");

        // calculate the texture positions
        Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
        backgroundPosition = new Vector2(
            (viewport.Width - backgroundTexture.Width) / 2,
            (viewport.Height - backgroundTexture.Height) / 2);
        imagePosition = backgroundPosition + new Vector2(158, 130);
        //descriptionAreaTextPosition = backgroundPosition + new Vector2(158, 350);
        gameLogoPosition = backgroundPosition + new Vector2(170, 80);
        bButtonPosition = backgroundPosition + new Vector2(225, 610);
        aButtonPosition = backgroundPosition + new Vector2(1120, 610);

        // set the textures on each menu entry
        //gameMenuEntry.Texture = buttonStartTexture;
        gameMenuEntry.SelectedTexture = buttonStartTexture;

        //optionsMenuEntry.Texture = button1Texture;
        optionsMenuEntry.SelectedTexture = buttonOptionsTexture;

        //creditsMenuEntry.Texture = button1Texture;
        creditsMenuEntry.SelectedTexture = buttonCreditsTexture;

        //exitMenuEntry.Texture = button1Texture;
        //exitMenuEntry.SelectedTexture = button1SelectedTexture;

        // now that they have textures, set the proper positions on the menu entries
        /*
        for (int i = 0; i < MenuEntries.Count; i++)
        {
            MenuEntries[i].Position = new Vector2(
                MenuEntries[i].Position.X,
                500f - ((MenuEntries[i].Texture.Height - 10) *
                    (MenuEntries.Count - 1 - i)));
        }
        */
        base.LoadContent();
    }

    public override void HandleInput()
    {
        if (InputManager.IsActionTriggered(InputManager.Action.Back))
        {
            ExitScreen();
            return;
        }

        base.HandleInput();
    }

    void NewGameMenuEntrySelected(object sender, EventArgs e)
    {
        ScreenManager.AddScreen(new StartGameScreen());        
    }

    void OptionsMenuEntrySelected(object sender, EventArgs e)
    {
        ScreenManager.AddScreen(new OptionsScreen());
    }

    void CreditsMenuEntrySelected(object sender, EventArgs e)
    {
        ScreenManager.AddScreen(new CreditsScreen());
    }

    public override void Draw(GameTime gameTime)
    {
        SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

        spriteBatch.Begin();

        // draw the background images
        spriteBatch.Draw(backgroundTexture, backgroundPosition, Color.White);
        //spriteBatch.Draw(imageTexture, imagePosition, Color.White);
        //spriteBatch.Draw(gameLogoTexture, gameLogoPosition, Color.White);

        // Draw each menu entry in turn.
        for (int i = 0; i < MenuEntries.Count; i++)
        {
            MenuEntry menuEntry = MenuEntries[i];
            bool isSelected = IsActive && (i == selectedEntry);
            menuEntry.Draw(this, isSelected, gameTime);
        }

        // draw the description text for the selected entry
       /* MenuEntry selectedMenuEntry = SelectedMenuEntry;
        if ((selectedMenuEntry != null) &&
            !String.IsNullOrEmpty(selectedMenuEntry.Description))
        {
            Vector2 textSize = new Vector2(10,10);
            //    Fonts.DescriptionFont.MeasureString(selectedMenuEntry.Description);
            Vector2 textPosition = descriptionAreaTextPosition + new Vector2(
                (float)Math.Floor((imageTexture.Width - textSize.X) / 2),
                0f);
            spriteBatch.DrawString(font,
                selectedMenuEntry.Description, textPosition, Color.White);
        }*/

        // draw the select instruction
        /*
         * spriteBatch.Draw(aButtonTexture, aButtonPosition, Color.White);
        spriteBatch.DrawString(font, "Select",
            new Vector2(
            aButtonPosition.X - font.MeasureString("Select").X - 5,
            aButtonPosition.Y + 5), Color.White);

        // if we are in-game, draw the back instruction
        spriteBatch.Draw(bButtonTexture, bButtonPosition, Color.White);
        spriteBatch.DrawString(font, "Resume",
            new Vector2(bButtonPosition.X + 55, bButtonPosition.Y + 5), Color.White);
        */

        spriteBatch.End();
    }

}
